package com.example.controller;

import com.example.common.api.CommonResult;
import com.example.domain.AdminUserDetails;
import com.example.mapper.AdminUserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private AdminUserMapper mapper;

    /**
     * 添加用户信息
     */
    @PostMapping("/create")
    public CommonResult<AdminUserDetails> save(@RequestBody AdminUserDetails user) {
        return CommonResult.success(mapper.getByUsername(user.getUsername()), "保存成功");
    }

    /**
     * 更新用户信息
     */
    @PostMapping("/update")
    public CommonResult<AdminUserDetails> update(@RequestBody AdminUserDetails user) {
        return CommonResult.success(mapper.getByUsername(user.getUsername()), "更新成功");
    }

    /**
     * 删除用户信息
     */
    @PostMapping("/delete/{name}")
    public CommonResult<AdminUserDetails> delete(@PathVariable("name") String name) {
        return CommonResult.success(mapper.getByUsername(name), "删除成功");
    }

    /**
     * 根据名称查询用户
     */
    @GetMapping("/get/{name}")
    public CommonResult<AdminUserDetails> get(@PathVariable("name") String name) {
        return CommonResult.success(mapper.getByUsername(name), "查询成功");
    }

    /**
     * 查询全部用户
     */
    @GetMapping("/list")
    public CommonResult<List<AdminUserDetails>> list() {
        return CommonResult.success(mapper.list(), "查询成功");
    }
}
